import { defineStore } from "pinia";
import { useRouter } from "vue-router";
const MENULIST = () => {
    return [
        {
            id: '1',
            title: "首页",
            icon: 'HomeFilled',
            children: [
                {
                    id: '6',
                    title: "数据概览",
                    path: '/Home/PersonalHomePage',
                    icon: 'DataAnalysis'
                },
                {
                    id: '7',
                    title: "公告管理",
                    path: '/Home/Notices',
                    icon: 'Message'
                }
            ]
        },
        {
            id: '2',
            title: "课程相关",
            icon: 'Notebook',
            children: [
                {
                    id: '8',
                    title: "课程管理",
                    icon: 'Management',
                    path: '/CourseManagement/Course'
                },
                {
                    id: '9',
                    title: "课程分类管理",
                    icon: 'Menu',
                    path: '/CourseManagement/courseCategory'
                }
            ]
        },
        {
            id: '3',
            title: "组织机构中心",
            icon: 'OfficeBuilding',
            children: [
                {
                    id: '10',
                    title: "企业管理",
                    icon: 'TrendCharts',
                    path: '/OrganizationManagement/Business'
                },
                {
                    id: '11',
                    title: "高校管理",
                    icon: 'School',
                    path: '/OrganizationManagement/University'
                }
            ]
        },
        {
            id: '4',
            title: "用户管理",
            icon: 'UserFilled',
            children: [
                {
                    id: '12',
                    title: "普通用户管理",
                    icon: 'User',
                    path: '/UserManagement/Account'
                },
                {
                    id: '13',
                    title: "管理员中心",
                    icon: 'Avatar',
                    path: '/UserManagement/Admin'
                }
            ]
        },
        {
            id: '5',
            title: "权限管理",
            icon: 'Stamp',
            children: [
                {
                    id: '14',
                    title: "权限管理",
                    icon: 'Tools',
                    path: '/AuthorityManagement/AuthorityAssignment'
                },
            ]
        }
    ]
}
export const useAppStore = defineStore('AppStore', {
    // id is required so that Pinia can connect the store to the devtools
    state: () => ({
        menuList: MENULIST(),
        setting: {
            theme: 'white',                // 导航栏主题
            uniqueOpened: true,       // 是否开启手风琴模式
            menuButton: true,         // 是否显示菜单展开按钮
            showCrumbs: true,         // 是否显示全局面包屑
            themeModel: 'auto',           // 主题模式
            showAvatar: true            // 是否显示头像
        },
        homePage: '/Home/PersonalHomePage',
        worktab: {
            current: {},  // 当前页
            opened: []    // 打开的页面
        },
        tempOpend: [],
        router: useRouter()
    }),
    getters: {
    },
    actions: {
        initState(info) {
            // 
        },
        addWorkTab(info) {
            if (!this.tempOpend.includes(info.name)) {
                this.tempOpend.push(info.name)
                this.worktab.opened.push(info)
            }
            this.worktab.current = info
            sessionStorage.setItem("worktab", JSON.stringify(this.worktab));
        },
        setUpTheme(theme) {
            this.setting.theme = theme
        },
        setLeftMenuUniqueOpened(info) {
            this.setting.uniqueOpened = info.show
        },
        setLeftMenuButton(info) {
            this.setting.menuButton = info.show
        },
        setLeftMenuCrumbs(info) {
            this.setting.showCrumbs = info.show
        },
        setThemeModel(themeModel) {
            this.setting.themeModel = themeModel
        },
        setshowAvatar(info) {
            this.setting.showAvatar = info.show
        },
        worktabinitState(info) {
            this.worktab = info
            this.tempOpend = []
            this.worktab.opened.forEach(item => {
                this.tempOpend.push(item.name)
            })
        },
        worktabRemove(info) {
            let { opened } = this.worktab
            let index = opened.findIndex(s => s.path === info.path)

            if (index > -1) {
                opened.splice(index, 1)
                this.tempOpend.splice(index, 1)
            }
            // 当页面全部关闭回到首页
            if (!opened.length && info.path != this.homePage) {
                this.router.push(this.homePage)
            }

            // 当前页
            if (opened.length && info.path === this.worktab.current.path) {
                this.router.push(this.worktab.opened[this.worktab.opened.length - 1].path)
            }
        },
        worktabRoute(info) {
            let { opened } = this.worktab
            let index = opened.findIndex(s => s.path === info.to.path)

            if (info.to.path === '/login') {
                return false
            }
            // console.log(e)
            // 新增tab
            if (index <= -1) {
                this.worktab.opened.push(info.to)
                this.worktab.current = info.to
            } else {  // 切换到当前tab
                this.worktab.current = opened[index]
            }
        },
        worktabRemoveOther(info) {
            this.worktab.opened = this.worktab.opened.filter((item) => {
                return item.path === info.path
            })
            console.log(this.worktab)
            this.tempOpend = []
            this.worktab.opened.forEach(item => {
                this.tempOpend.push(item.name)
            })
            console.log(this.tempOpend)
            sessionStorage.setItem("worktab", JSON.stringify(this.worktab));
        },
        worktabRemoveLeft(info) {
            let currentPath = info.path;
            let list = this.worktab.opened;

            for (let i = 0; i < list.length; i++) {
                let item = list[i];
                if (item.path === currentPath) {
                    this.worktab.opened.splice(0, i)
                    this.tempOpend.splice(0, i)
                    break;
                }
            }
            sessionStorage.setItem("worktab", JSON.stringify(this.worktab));
        },
        worktabRemoveRight(info) {
            let currentPath = info.path;
            let list = this.worktab.opened;

            for (let i = 0; i < list.length; i++) {
                let item = list[i];
                if (item.path === currentPath) {
                    this.worktab.opened.splice(i + 1)
                    this.tempOpend.splice(i + 1)
                    break;
                }
            }
            sessionStorage.setItem("worktab", JSON.stringify(this.worktab));
        },
        worktabRemoveAll() { // 关闭全部页面并跳转到首页
            this.worktab = {
                current: { name: "数据概览", path: "/Home/PersonalHomePage" },
                opened: [{ name: "数据概览", path: "/Home/PersonalHomePage" }]
            }
            this.tempOpend = ["数据概览"]
            this.router.push(this.homePage)
        },
        // 解决重新登录菜单栏被改变bug，退出帐号前重新赋值
        resetMenuList() {
            this.menuList = MENULIST()
        }
    }
});
